package es.usj;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;

public class ServicioAlarmas extends Service {

	private NotificationManager mManager;

    ReceptorBroadcast alarma = new ReceptorBroadcast();

    @Override
    public void onCreate()
    {
        super.onCreate();       
    }

    /*
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
	{
         alarma.SetAlarm(ServicioAlarmas.this, null, 0);
         return START_STICKY;
	}

    public void onStart(Context context,Intent intent, int startId)
    {
        alarma.SetAlarm(context, null, 0);
    }*/

    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }
    
	   @SuppressWarnings("static-access")
	   @Override
	   public void onStart(Intent intent, int startId)
	   {
	       super.onStart(intent, startId);
	      
	       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
	       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
	     
	       Notification notification = new Notification(R.drawable.ic_launcher,"Sprint1: Mensaje de alarma!", System.currentTimeMillis());
	       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
	 
	       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
	       notification.flags |= Notification.FLAG_AUTO_CANCEL;
	       
	       //TODO getBundle para sacar el nombre de la alarma.
/* esto hace que explote	       Bundle bundle = intent.getExtras();	       
	       String descripcionAlarma = bundle.getString("descripcion", "Mensaje de Alarma!");*/
	       notification.setLatestEventInfo(this.getApplicationContext(), "Sprint1", "Mensaje de Alarma!", pendingNotificationIntent);
	       notification.defaults |= Notification.DEFAULT_SOUND;
	       notification.defaults |= Notification.DEFAULT_VIBRATE;
	       mManager.notify(0, notification);
	    }
	 
	    @Override
	    public void onDestroy()
	    {
	        // TODO Auto-generated method stub
	        super.onDestroy();
	    }
}
